﻿using PaddleClasSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace PaddleClasSharpDemo
{
    /// <summary>
    /// PaddleClasSharp示例
    /// </summary>
    public partial class MainForm : Form
    {
        private string[] bmpFilters = new string[] { ".bmp", ".jpg", ".jpeg", ".tiff", ".tif", ".png" };
        private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tif;*.png";
        private string title = "PaddleClas v4.4.0 C# Demo  QQ群：318860399，定制开发QQ:277784829   by 广州英田信息科技有限公司";
        private PaddleClasEngine engine;
        Bitmap bmp;
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        public MainForm()
        {
            InitializeComponent();
            this.Text = title;
            imageView1.AllowDrop = true;
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            ClasParameter parameter = new ClasParameter();
            parameter.rec_resize_size = 224;
            engine = new PaddleClasEngine(null, parameter);
        }
        private Bitmap GetClipboardImage()
        {
            bmp = (Bitmap)Clipboard.GetImage();
            if (bmp == null)
            {
                var files = Clipboard.GetFileDropList();

                string[] Filtersarr = new string[files.Count];
                files.CopyTo(Filtersarr, 0);
                Filtersarr = Filtersarr.Where(x => bmpFilters.Contains(Path.GetExtension(x).ToLower())).ToArray();
                if (Filtersarr.Length > 0)
                {
                    var imagebyte = File.ReadAllBytes(Filtersarr[0]);
                    bmp = new Bitmap(new MemoryStream(imagebyte));
                }
            }
            return bmp;
        }
        private void imageView1_DragDrop(object sender, DragEventArgs e)
        {
            var data = e.Data;
            if (data == null) return;
            string[] files = data.GetData(DataFormats.FileDrop, autoConvert: true) as string[];

            string[] Filtersarr = new string[files.Count()];
            files.CopyTo(Filtersarr, 0);
            Filtersarr = Filtersarr.Where(x => bmpFilters.Contains(Path.GetExtension(x).ToLower())).ToArray();
            if (Filtersarr.Length > 0)
            {
                var imagebyte = File.ReadAllBytes(Filtersarr[0]);
                bmp = new Bitmap(new MemoryStream(imagebyte));
                imageView1.Image = bmp;

                richTextBox1.Clear();
                richTextBox1.Show();
                dataGridView1.Hide();
                if (bmp == null) return;
                dt1 = DateTime.Now;
                var result = engine.Detection(imagebyte);
                dt2 = DateTime.Now;
                ShowResult(result);

            }
        }
       
        
        private void imageView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
     
        private void imageView1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }
        /// <summary>
        /// 打开本地图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripopenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            var imagebyte = File.ReadAllBytes(ofd.FileName);
            bmp = new Bitmap(new MemoryStream(imagebyte));
            imageView1.Image = bmp;

            richTextBox1.Clear();
            richTextBox1.Show();
            dataGridView1.Hide();
            if (bmp == null) return;

            dt1 = DateTime.Now;
            var result = engine.Detection(imagebyte);
            dt2 = DateTime.Now;
            ShowResult(result);

        }

        /// <summary>
        /// 识别截图文本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            this.Hide();
            System.Threading.Thread.Sleep(200);
            Application.DoEvents();
            richTextBox1.Clear();
            richTextBox1.Show();
            dataGridView1.Hide();
            ScreenCapturer.ScreenCapturerTool screenCapturer = new ScreenCapturer.ScreenCapturerTool();
            if (screenCapturer.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                bmp = (Bitmap)screenCapturer.Image;
                imageView1.Image = bmp;
                try
                {
                    dt1 = DateTime.Now;
                    var result = engine.Detection(bmp);
                    dt2 = DateTime.Now;
                    ShowResult(result);
                }
                catch (Exception ex)
                {
                }

            }
            this.Show();
        }
        /// <summary>
        /// 剪切板识别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>       
        private void toolStripsnapocr_Click(object sender, EventArgs e)
        {
            bmp = GetClipboardImage();

            imageView1.Image = bmp;
            
                try
            {
                dt1 = DateTime.Now;
                var result = engine.Detection(bmp);
                dt2 = DateTime.Now;
                ShowResult(result);

            }
                catch (Exception ex)
                {
                }
        }
        /// <summary>
        /// 显示结果
        /// </summary>
        private void ShowResult(ClasResult result)
        {
            
            richTextBox1.Clear();
            Bitmap bitmap = (Bitmap)this.imageView1.Image;
            if (result == null || result.ObjectResults == null) return;


                foreach (var item in result.ObjectResults)
                {
                    richTextBox1.AppendText(item.name + "\n");

                }
            try
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    foreach (var item in result.ObjectResults)
                    {
                        g.DrawRectangle(new Pen(Brushes.Red, 2), item.Rectangle);
                    }
                }
                imageView1.Image = null;
                imageView1.Image = bitmap;
            }
            catch (Exception ex)
            {
                Bitmap bmp = new Bitmap(bitmap);

                using (Graphics g = Graphics.FromImage(bmp))
                {
                    foreach (var item in result.ObjectResults)
                    {
                        g.DrawRectangle(new Pen(Brushes.Red, 2), item.Rectangle);
                    }
                }
                imageView1.Image = null;
                imageView1.Image = bmp;
            }
            richTextBox1.AppendText("-----------------------------------\n");
            richTextBox1.AppendText($"耗时：{(dt2 - dt1).TotalMilliseconds}ms\n");  
        }
    }
}
